//index.js
//获取应用实例
const app = getApp()
const http = require('../../utils/http.js')
const config = require('../../utils/config.js')

Page({
  data: {
    config,
    // 头像列表数据
    avatarList: [],
    // 头像底图相关状态
    currentFaceType: 1, // 1:男性, 2:女性, 3:通用
    currentPage: 1,
    pageSize: 12,
    totalPages: 1,
    selectedAvatarId: null, // 存储选中的头像ID
    resourcesUrl: app.globalData.resourcesUrl || '', // 资源URL
    // 轮播图数据
    indexImgs: [],
    seq: [],
    autoplay: true,
    interval: 3000,
    duration: 500,
    indicatorColor: 'rgba(255, 255, 255, 0.5)',
    indicatorActiveColor: 'rgba(255, 255, 255, 1)',
  },

  onLoad: function () {
    // 页面加载时的初始化操作
    console.log('捏脸小程序首页加载')
    this.loadFaceBases()
    // 加载轮播图
    this.getIndexImgs()
  },

    //加载轮播图
  getIndexImgs() {
    //加载轮播图
    var params = {
      url: "/indexImgs",
      method: "GET",
      data: {},
      callBack: (res) => {
        this.setData({
          indexImgs: res,
          seq: res
        });
      }
    };
    http.request(params);
  },

  // 获取人脸底图列表
  loadFaceBases: function () {
    const that = this
    try {
      const params = {
        url: '/faceBase/page',
        method: 'GET',
        data: {
          page: that.data.currentPage,
          limit: that.data.pageSize,
          faceType: 3,
          status: 1 // 只获取启用的底图
        },
        header: {
          'Content-Type': 'application/json',
          // 'Authorization': '80e46f24-cdf5-4cac-bb7e-2f7fd0f4d71b' // 如果需要认证
        },
        callBack: res => {
          console.log(res)
          if (res) {
            console.log('获取人脸底图成功:', res)
            // 转换接口数据格式
            res.records.forEach((item, index) => {
              item.faceImageUrl = that.data.resourcesUrl + item.faceImageUrl
              item.image = item.faceImageUrl // 兼容原有的image字段
              item.label = item.faceName // 兼容原有的label字段
            })
            that.setData({
              avatarList: res.records,
              totalPages: Math.ceil(res.total / that.data.pageSize)
            })
          }
        }
      }
      http.request(params)
    } catch (error) {
      console.error('获取人脸底图异常:', error)
    }
  },

  // 切换性别
  switchFaceType: function (e) {
    const type = Number(e.currentTarget.dataset.type)
    this.setData({
      currentFaceType: type,
      currentPage: 1,
      selectedAvatarId: null
    })
    this.loadFaceBases()
  },

  // 加载更多头像
  loadMoreAvatars: function () {
    if (this.data.currentPage < this.data.totalPages) {
      this.setData({
        currentPage: this.data.currentPage + 1
      })
      this.loadFaceBases()
    }
  },

  // 刷新头像数据
  refreshAvatars: function () {
    this.setData({
      currentPage: 1,
      selectedAvatarId: null
    })
    this.loadFaceBases()
  },

  // 选择头像
  selectAvatar: function (e) {
    const index = e.currentTarget.dataset.index
    const avatar = this.data.avatarList[index]
    this.setData({
      selectedAvatarId: avatar.faceBaseId || avatar.id
    })
    console.log('选择了头像:', this.data.selectedAvatarId)
  },

  // 开始定制
  startCustomize: function () {
    if (!this.data.selectedAvatarId) {
      wx.showToast({
        title: '请先选择一个玩偶',
        icon: 'none'
      })
      return
    }

    console.log('开始定制头像，选中的头像ID:', this.data.selectedAvatarId)
    // 跳转到定制页面并传递选中的头像ID
    wx.navigateTo({
      url: `/pages/customize/customize?avatarId=${this.data.selectedAvatarId}`
    })
  },

  onPullDownRefresh: function () {
    // 下拉刷新处理
    this.refreshAvatars()
    wx.stopPullDownRefresh()
  }
})